<?php

class PollsController extends Zend_Controller_Action {

    public function preDispatch() {
        if (!defined('PAGE_ID'))
            $this->_redirect(SITE_URL);
    }

    public function indexAction() {
        (int) $sid = $this->getRequest()->getParam('id', 0);
        $model = new PollsModel();
        $this->view->poll = $model->getLastPoll($sid);
        if (!is_null($this->view->poll)) {
            $userIP = $_SERVER['REMOTE_ADDR'];
            $this->view->answers = $model->getAnswers($this->view->poll->id);
            if (!$model->alreadyVote($this->view->poll->id,$userIP)){
                $this->view->already = false;
                if ($this->_request->isPost()) {
                    $formData = $this->_request->getPost();
                    $choice = (int) $formData['choice'];
                    if ($choice > 0) {
                        $vote = array(
                            'pid' => $this->view->poll->id,
                            'cid' => $choice,
                            'host' => $userIP
                        );
                        if (defined('USER_ID')){
                            $vote['uid'] = USER_ID;
                        }
                        $model->vote($vote);
                        $this->indexAction();
                    }
                }
            } else {
                $this->view->already = true;
            }
        }
    }

    public function modifyAction() {
        $action = $this->getRequest()->getParam('a', '');
        $page = (int) $this->getRequest()->getParam('page', 1);
        switch ($action) {
            case 'new':
                $this->_forward('new');
                break;
            case 'edit':
                $this->_forward('edit');
                break;
            case 'remove':
                $this->_forward('remove');
                break;
            default:
                (int) $sid = $this->getRequest()->getParam('id', 0);
                $model = new PollsModel();
                $this->view->poll = $model->getLastPoll($sid);
                if (!is_null($this->view->poll)) {
                    $this->view->answers = $model->getAnswers($this->view->poll->id);
                }
                $this->view->polls = $model->getPaginatorPolls($page, $sid);
        }
    }

    public function newAction() {
        $sid = (int) $this->getRequest()->getParam('id', 0);
        $model = new PollsModel();
        $forms = new PollsForm();
        $form = $forms->newPoll($sid);
        $this->view->form = $form;
        if ($this->_request->isPost()) {//Форма отправлена
            $formData = $this->_request->getPost();
            if ($form->isValid($formData)) {
                $question = array(
                    'sid' => $sid,
                    'question' => $formData['question'],
                    'description' => $formData['description'],
                    'active' => $formData['active'],
                    'date' => strtotime($formData['date'])
                );
                $answers = array($formData['answer'], $formData['answer2']);
                if (isset($formData['answer3']))
                    $answers = array_merge($answers, $formData['answer3']);
                $questionID = $model->updateQuestion($question);
                $model->addAnswers($answers, $questionID);
                $this->_redirect('/pages/modify/id/' . $sid);
            } else {
                $form->populate($formData);
            }
        }
    }

    public function editAction() {
        $sid = (int) $this->getRequest()->getParam('id', 0);
        $pid = (int) $this->getRequest()->getParam('pid', 0);
        $model = new PollsModel();
        $forms = new PollsForm();
        if ($this->_request->isPost()) {//Форма отправлена
            $formData = $this->_request->getPost();
            $form = $forms->editPoll($sid, $pid);
            if ($form->isValid($formData)) {
                $question = array(
                    'sid' => $sid,
                    'question' => $formData['question'],
                    'description' => $formData['description'],
                    'active' => $formData['active']
                );
                $votes = $formData['votes'];
                $answers = array();
                foreach ($formData['answer'] as $key => $value)
                    if (trim($value) <> '')
                        $answers[] = (object) array('id' => $key, 'answer' => $value, 'votes' => $votes[$key]);

                if (count($answers) > 0) {
                    $this->view->answers = $answers;
                    $model->updateQuestion($question, $pid);
                    $model->updateAnswers($answers, $pid);
                }
                $this->_redirect('/pages/modify/id/' . $sid);
            } else {
                $form->populate($formData);
                $answers = $model->getAnswers($pid);
                $this->view->answers = $answers;
            }
        } else {
            $poll = $model->getPolls($sid, $pid);
            $answers = $model->getAnswers($pid);
            $form = $forms->editPoll($sid, $pid, $answers);
            $form->populate(array(
                'question' => $poll->question,
                'description' => $poll->description,
                'active' => $poll->active
                    )
            );
            $this->view->answers = $answers;
        }
        $this->view->form = $form;
    }

    public function removeAction() {

    }

    public function addAction() {
        $pid = (int) $this->getRequest()->getParam('pid', 0);
        $this->_redirect('/pages/section/id/' . $pid);
    }

}